﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;

namespace DAL
{
    public class CardDAL:Base<Card>
    {
        public CardDAL()
            : base("card", "CID")
        {
        }

        public override bool Insert(Card entity)
        {
            string sql = "insert into `card` (`AID`,`Type`,`Title`,`Money`,`Description`,`CardNumber`,`OID`,`Credit_Line`,`Bill_Date`,`Due_Date`) values (@aid,@type,@title,@money,@desc,@num,@oid,@cl,@bd,@dd)";

            var r = MySQLHelper.ExecuteNonQuery(sql,
                GetParams("aid", entity.AID),
                GetParams("type", entity.Type),
                GetParams("title", entity.Title),
                GetParams("money", entity.Money),
                GetParams("desc", entity.Description),
                GetParams("num", entity.CardNumber),
                GetParams("oid", entity.OrganizationID),
                GetParams("cl", entity.CreditLine),
                GetParams("bd", entity.BillDate),
                GetParams("dd", entity.DueDate)
                );

            entity.CID = GetLastInsertID();
            return r == 1;
        }

        public DataTable GetCardsByAID(int aid)
        {
            string sql = "select * from card where aid=" + aid;
            return MySQLHelper.Query(sql);
        }

        public override bool Update(Card entity)
        {
            string sql = "update card set type=@type,title=@title,money=@money,description=@desc,cardnumber=@num,oid=@oid,credit_line=@cl,bill_date=@bd,due_date=@dd where cid = @cid";
            var r = MySQLHelper.ExecuteNonQuery(sql,
                GetParams("aid", entity.AID),
                GetParams("type", entity.Type),
                GetParams("title", entity.Title),
                GetParams("money", entity.Money),
                GetParams("desc", entity.Description),
                GetParams("num", entity.CardNumber),
                GetParams("oid", entity.OrganizationID),
                GetParams("cl", entity.CreditLine),
                GetParams("bd", entity.BillDate),
                GetParams("dd", entity.DueDate),
                GetParams("cid", entity.CID)
                );
            return r == 1;
        }

        public override Card GetEntityById(int id)
        {
            throw new NotImplementedException();
        }

        public override Card GetEntityById(string id)
        {
            throw new NotImplementedException();
        }
    }
}
